package com.camartsApp.Config;

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClients; // 注意这里是 MongoClients 工厂类
import com.mongodb.client.MongoClient; // 这个是正确的 MongoClient 接口
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;

@Configuration
public class MongoConfig {

    private static final String CONNECTION_STRING = "mongodb://root:A1b%40C3d%23E4f%26G5h%2aI6j@dds-uf624a22e0a41d841657-pub.mongodb.rds.aliyuncs.com:3717,dds-uf624a22e0a41d842824-pub.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-90448167";
    private static final String DATABASE_NAME = "software_db";

    @Bean
    public MongoClient mongoClient() {
        ConnectionString connString = new ConnectionString(CONNECTION_STRING);
        MongoClientSettings settings = MongoClientSettings.builder()
                .applyConnectionString(connString)
                .build();
        return MongoClients.create(settings);
    }

    @Bean
    public MongoDatabaseFactory mongoDatabaseFactory() {
        return new SimpleMongoClientDatabaseFactory(mongoClient(), DATABASE_NAME);
    }

    @Bean
    public MongoTemplate mongoTemplate() {
        return new MongoTemplate(mongoDatabaseFactory());
    }
}
